package com.usian.listener;
import com.usian.service.SearchItemService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ListenerMsg {

    @Autowired
    private SearchItemService searchItemService;

    //添加同步
    @RabbitListener(bindings = @QueueBinding(
            //交换机名称，是否持久化
            value = @Queue(value = "search_delete", durable = "true"),
            //监听队列，指定消息类型
            exchange = @Exchange(value = "exchange_usian", type = ExchangeTypes.TOPIC),
            //路由规则
            key = "item.delete"
    ))
    public void delete(String msg) throws Exception {
        int i=   searchItemService.deleteIndex(msg);
        if (i >0) {
            throw new RuntimeException("同步异常");
        }
    }

    //添加同步
    @RabbitListener(bindings = @QueueBinding(
            //交换机名称，是否持久化
            value = @Queue(value = "search_insert", durable = "true"),
            //监听队列，指定消息类型
            exchange = @Exchange(value = "exchange_usian", type = ExchangeTypes.TOPIC),
            //路由规则
            key = "item.insert"
    ))
    public void insert(String msg) throws Exception {
        int i = searchItemService.insertIndex(msg);
        if (i > 0) {
            throw new RuntimeException("同步异常");
        }
    }

    // 添加同步
    @RabbitListener(bindings = @QueueBinding(
            //交换机名称，是否持久化
            value = @Queue(value = "search_update", durable = "true"),
            //监听队列，指定消息类型
            exchange = @Exchange(value = "exchange_usian", type = ExchangeTypes.TOPIC),
            //路由规则
            key = "item.update"
    ))
    public void update(String msg) throws Exception {
        int i = searchItemService.updateIndex(msg);
        if (i > 0) {
            throw new RuntimeException("同步异常");
        }
    }


}
